package graphics;

import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;

import playlist.Crawler;

import function.ChainMp3;
import function.Mp3File;

public class SearchChoiceInput {

	private static final int MAX_WIDTH = 210;
	private static int X_SEARCH_TEXT_OFFSET = 20;
	private static int NB_INPUTS_PER_PAGE = 7;

	Mp3File[] choices = null;
	int pageIndex = 0;
	private Graphics graphics = null;

	public SearchChoiceInput(ChainMp3 chainChoices) {
		choices = new Mp3File[chainChoices.getLength()];
		ChainMp3 current = chainChoices;

		for (int i = 0; i < choices.length; i++) {
			choices[i] = current.getMp3File();
			current = current.getNext();
		}
	}

	public SearchChoiceInput(Mp3File[] mp3s) {
		choices = mp3s;
	}

	public void repaint(Graphics someGraphics) {
		graphics = someGraphics;

		graphics.fillRect(0, 0, 240, 250);

		int nbChoices = getNumberOfChoices();
		int textHeight = graphics.getFont().getHeight();

		int listOffset = 3 + textHeight;

		if ((severalPages()) && (pageIndex > 0)) {
			graphics.setColor(Theme.getMenuColor());
			graphics.drawString("More", 20, listOffset - textHeight,
					Graphics.LEFT | Graphics.TOP);
		}

		for (int i = 0; i < nbChoices; i++) {
			String rawchoice = getRawChoice(i);
			String choice = resize(rawchoice);

			graphics.setColor(CrawlerColor.WHITE);
			graphics.drawString(choice, X_SEARCH_TEXT_OFFSET, listOffset
					+ textHeight * i, Graphics.LEFT | Graphics.TOP);
		}

		if ((severalPages()) && (!isLastPage())) {

			graphics.setColor(Theme.getMenuColor());
			graphics.drawString("More", 20,
					listOffset + textHeight * nbChoices, Graphics.LEFT| Graphics.TOP);
		}
		graphics.setColor(Theme.getMenuColor());
		graphics.drawString((pageIndex + 1) + "/" + getPagesNumber(), 240,
				listOffset + textHeight * NB_INPUTS_PER_PAGE, Graphics.RIGHT| Graphics.TOP);

	}

	private String getRawChoice(int i) {
		return (i + 1) + ". " + choices[NB_INPUTS_PER_PAGE * pageIndex + i].getTitle();
	}

	private String resize(String rawchoice) {
		Font f = graphics.getFont();
		String result = rawchoice;
		if (isOneLine(result)) {
			return result;
		}

		while (f.stringWidth(result + " [...]") > MAX_WIDTH) {
			result = result.substring(0, result.lastIndexOf(' '));
		}
		return result + " [...]";
	}
	
	private boolean isOneLine(String input) {
		Font f = graphics.getFont();
		return (f.stringWidth(input) < MAX_WIDTH);
	}

	public void processKey(int keyCode) {

		if (getPagesNumber() > 1) {
			if ((keyCode == -2) && (!isLastPage())) {
				pageIndex++;
			} else if ((keyCode == -1) && (pageIndex != 0)) {
				pageIndex--;
			}
		}

		int offset = 49;
		int nbChoices = getNumberOfChoices();

		if ((keyCode < 49) || (keyCode > 49 + nbChoices - 1)) {
			return;
		}

		Mp3File mp3 = choices[pageIndex * NB_INPUTS_PER_PAGE + keyCode - offset];
		
		if (isOneLine(getRawChoice(keyCode - offset))) {			
			Crawler.getInstance().searchChoiceCompleted(mp3);
		} else {
			Crawler.getInstance().popUp(mp3);
		}
	}

	private int getPagesNumber() {
		if (choices.length <= NB_INPUTS_PER_PAGE) {
			return 1;
		} else {
			if (choices.length % NB_INPUTS_PER_PAGE == 0) {
				return ((int) choices.length / NB_INPUTS_PER_PAGE);
			} else {
				return ((int) choices.length / NB_INPUTS_PER_PAGE) + 1;
			}
		}
	}

	private boolean severalPages() {
		return getPagesNumber() > 1;
	}

	private int getNumberOfChoices() {
		if (getPagesNumber() == 1) {
			return choices.length;
		} else if (isLastPage()) {
			if (choices.length % NB_INPUTS_PER_PAGE == 0) {
				return NB_INPUTS_PER_PAGE;
			} else {
				return choices.length % NB_INPUTS_PER_PAGE;
			}
		} else {
			return NB_INPUTS_PER_PAGE;
		}
	}

	private boolean isLastPage() {
		return pageIndex == getPagesNumber() - 1;
	}
	
	public void gotoLastPage() {
		pageIndex = getPagesNumber() - 1;
	}
}
